package server.service;

import common.Message;
import common.MessageStatus;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class UserThread extends Thread{
    private Socket socket;

    //客户端的没有-----待思考？
    private String useId;
    public UserThread(String useId,Socket socket) {
        this.useId=useId;
        this.socket = socket;
    }

    @Override
    public void run() {
        
        while(true){
            
            //把本线程输出流放在这里，再打开另一个用户的线程socket的输出流时客户端会出问题
            try {
                System.out.println("等待 "+useId+" 发送消息");

                ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
                Message message =(Message) objIn.readObject();

                if(message.getType()==MessageStatus.USERID_ERROR){
                    message.setType(MessageStatus.USERID_ERROR);

                    ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
                    objOut.writeObject(message);
                    objOut.flush();
                }
                else if(message.getType()==MessageStatus.PASSWORD_ERROR){
                    message.setType(MessageStatus.PASSWORD_ERROR);

                    ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
                    objOut.writeObject(message);
                    objOut.flush();
                }
                else if(message.getType()==MessageStatus.GET_ONLINE_FRIEND){
                    System.out.println(useId+" 请求要在线好友列表");
                    message.setType(MessageStatus.RETURN_ONLINE_FRIEND);

                    //好像没必要
                    message.setGetter(message.getSender());

                    String onlineUser = SocketThreadManager.getOnlineUser(useId);
                    message.setContent(onlineUser);

                    ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
                    objOut.writeObject(message);
                    objOut.flush();

                    System.out.println("发送在线好友列表给" +useId);
                }
                else if(message.getType()==MessageStatus.NORMAL_EXIT){
                    message.setType(MessageStatus.NORMAL_EXIT_SUCCEED);

                    ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
                    objOut.writeObject(message);
                    objOut.flush();

                    SocketThreadManager.removeUserThread(useId);
                    System.out.println(useId+" 正常退出");
                    break;
                }else if(message.getType()==MessageStatus.PRIVATE_CHAT){
                    System.out.println(message.getSender()+" 请求发送消息给 "+message.getGetter());

                    UserThread getterThread = SocketThreadManager.getUserThread(message.getGetter());

                    //如果自己发给自己，要不要发？

                    //好友不在线，保存到hashMap
                    if (getterThread==null){
                        System.out.println(message.getGetter()+" 不在线，保存到服务器");
                        ServerService.messages.get(message.getGetter()).add(message);
                        continue;
                    }

                    //转发给Client端getter的线程
                    System.out.println("转发 "+message.getSender()+" 的消息给 "+message.getGetter());
                    Socket getterSocket = getterThread.getSocket();


                    ObjectOutputStream getterObjOut = new ObjectOutputStream(getterSocket.getOutputStream());
                    getterObjOut.writeObject(message);
                    getterObjOut.flush();

                }else if(message.getType()==MessageStatus.GROUP_CHAT){

                    //没处理不在线的保存到数据库
                    System.out.println(message.getSender()+" 请求发送消息给 "+message.getGetter());

//                    HashMap<String, UserThread> users = SocketThreadManager.getUsers();
//                    Iterator<String> userIdIterator = users.keySet().iterator();

                    Iterator<String> userIdIterator = ServerService.messages.keySet().iterator();

                    System.out.println("转发 " + message.getSender() + " 的消息给 " + message.getGetter());
                    while(userIdIterator.hasNext()) {
                        String otherUser=userIdIterator.next();

                        //不给自己转发
                        if(otherUser.equals(message.getSender()))continue;

                        UserThread userThread = SocketThreadManager.getUserThread(otherUser);

                        //好友不在线，保存到hashMap
                        if(userThread==null){
                            System.out.println(otherUser+" 不在线，保存到服务器");
                            ServerService.messages.get(otherUser).add(message);
                            continue;
                        }

                        //转发给Client端getter的线程
                        Socket getterSocket = userThread.getSocket();

                        ObjectOutputStream getterObjOut = new ObjectOutputStream(getterSocket.getOutputStream());
                        getterObjOut.writeObject(message);
                        getterObjOut.flush();
                    }
                }else if(message.getType()==MessageStatus.PRIVATE_SEND_FILE){
                    System.out.println(message.getSender()+" 请求发送文件给 "+message.getGetter());

                    UserThread getterThread = SocketThreadManager.getUserThread(message.getGetter());
                    if(getterThread==null){
                        System.out.println(message.getGetter()+" 不在线，保存到数据库");
                        continue;
                    }
                    System.out.println("转发 "+message.getSender()+" 的文件给 "+message.getGetter());

                    ObjectOutputStream objOut = new ObjectOutputStream(getterThread.getSocket().getOutputStream());
                    objOut.writeObject(message);
                    objOut.flush();
                }else if(message.getType()==MessageStatus.GROUP_SEND_FILE){

                }
                else {
                    System.out.println("其他");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public String getUseId() {
        return useId;
    }
}
